import { AppComponentBase } from '@shared/common/app-component-base';
import { LoginVM } from './../../../shared/service-proxy/service-proxies';
import { Component, OnInit, Injector } from '@angular/core';
import { Router, ActivatedRoute } from '@angular/router';
import { LoginService } from './login.service'
import {
  FormBuilder,
  FormGroup,
  Validators
} from '@angular/forms';

@Component({
  selector: 'app-login',
  templateUrl: './login.component.html',
  styleUrls: ['./login.component.less']
})
export class LoginComponent extends AppComponentBase implements OnInit {

  validateForm: FormGroup;
  model: LoginVM = new LoginVM();

  constructor(injector:Injector,
    private fb: FormBuilder,
    private loginService: LoginService,
    private _router: Router) {
      super(injector);
  }

  submitForm(): void {
    // tslint:disable-next-line:forin
    for (const i in this.validateForm.controls) {
      this.validateForm.controls[i].markAsDirty();
      this.validateForm.controls[i].updateValueAndValidity();
    }

    this.login();

  }

  ngOnInit(): void {
    this.validateForm = this.fb.group({
      username: [null, [Validators.required]],
      password: [null, [Validators.required]],
      remember: [true]
    });
  }
  login(): void {
    for (const i in this.validateForm.controls) {
      this.validateForm.controls[i].markAsDirty();
      this.validateForm.controls[i].updateValueAndValidity();
    }

    if(this.validateForm.valid)
    {
      this.loginService.authenticate(this.model);
    }
    else { // 验证未通过
      this.message.error(this.l('verify_failed_please_check_the_form_item'));
    }
   
  }
}
